/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Main.Empieza;
import Modelo.*;
import java.util.ArrayList;
import javax.swing.JPasswordField;

/**
 *
 * @author Paco Amoros & Samuel Ruiz
 */
public class ControlAcceso extends javax.swing.JFrame {

    private Usuario usuario;
    private UsuarioGenerico ug = new UsuarioGenerico("def", "def");
    private UsuarioRegistrado ur = new UsuarioRegistrado("def", "def");
    private Administrador us = new Administrador("def", "def");
    /**
     * Creates new form Vista
     */
    public ControlAcceso(GestorTicketCine gtk) {
        initComponents();
        this.gestorticketCine = gtk;
        tk = gtk.getTicketCine();
        //this.setActionListener(gtk);
        //usuario.iniciarConsultar(provincia);
        boolean found = false;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTcontraseña = new javax.swing.JPasswordField();
        jBaceptar = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();
        jTusuario = new javax.swing.JTextField();
        userNoRegistradoButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Entrar al sistema");

        jLabel1.setText("Usuario");

        jLabel2.setText("Contraseña");

        jBaceptar.setText("Aceptar");
        jBaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBaceptarActionPerformed(evt);
            }
        });
        jBaceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBaceptarKeyPressed(evt);
            }
        });

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });

        jTusuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTusuarioActionPerformed(evt);
            }
        });

        userNoRegistradoButton.setText("Consultar Cines");
        userNoRegistradoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNoRegistradoButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Comprar Entrada");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel1)
                        .addGap(36, 36, 36))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jBaceptar)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userNoRegistradoButton)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jTusuario)
                        .addComponent(jTcontraseña)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(cancelarButton)
                            .addGap(18, 18, 18)
                            .addComponent(jButton1))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTcontraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBaceptar)
                    .addComponent(cancelarButton)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(userNoRegistradoButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBaceptarActionPerformed
        System.out.println("Aceptar " + jTusuario.getText() + " - " + jTcontraseña.getText());
        //Usuario us = new Usuario (jTusuario.getText(), jTcontraseña.getText());

        boolean found = false; //Si encuentra un usuario = true
        String modo = "default";
        usuariosGenericos = tk.getUsuariosgenericos();
        administradores = tk.getAdministradores();
        usuariosregistrados = tk.getUsuariosregistrados();

        System.out.println("Usuarios : " + usuariosGenericos.size());
        System.out.println("Administradores : " + administradores.size());
        System.out.println("Usuarios Registrados: " + usuariosregistrados.size());

        for (UsuarioGenerico p : usuariosGenericos) {
            System.out.println(p);
            if (p.getNombre().equals(jTusuario.getText()) && p.getContrasenya().equals(jTcontraseña.getText())) {
                ug = p;
                modo = ug.getClass().getName();
                System.out.println("Usuario encontrado");
                found = true;
            }

        }
        if (!found) {
            for (Administrador a : administradores) {
                System.out.println(a);
                if (a.getNombre().equals(jTusuario.getText()) && a.getContrasenya().equals(jTcontraseña.getText())) {
                    
                    us = a;
                    System.out.println("Administrador encontrado");
                    modo = us.getClass().getName();
                    found = true;
                }

            }
        }
        if (!found) {
            for (UsuarioRegistrado uR : usuariosregistrados) {
                System.out.println(uR);
                if (uR.getNombre().equals(jTusuario.getText()) && uR.getContrasenya().equals(jTcontraseña.getText())) {
                    ur = uR;
                    modo = ur.getClass().getName();
                    System.out.println("Usuario Registrado encontrado");
                    found = true;
                }

            }
        }

        switch (modo) {
            case "Modelo.UsuarioGenerico":
                /*    this.setVisible(false);
                 Vista.ConsultarCines u = new Vista.ConsultarCines(gestorticketCine);
                 u.setVisible(true);*/
                break;

            case "Modelo.Administrador":
                this.setVisible(false);
                Vista.DarAltaPelicula a = new Vista.DarAltaPelicula(gestorticketCine);
                a.setVisible(true);
                break;

            case "Modelo.UsuarioRegistrado":
                this.setVisible(false);
                Avisar1hTimer avisar  = new Avisar1hTimer(ur);
                Vista.VentanaUsuarioRegistrado vur = new Vista.VentanaUsuarioRegistrado(gestorticketCine,ur);
                vur.setVisible(true);
                break;

            default:
                Error err = new Error("Usuario no encontrado");
        }


    }//GEN-LAST:event_jBaceptarActionPerformed

    private void userNoRegistradoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userNoRegistradoButtonActionPerformed
        // TODO add your handling code here:
        consultarCines = new ConsultarCines(gestorticketCine);
        consultarCines.setVisible(true);
    }//GEN-LAST:event_userNoRegistradoButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        setVisible(false);
        ComprarEntrada ca = new ComprarEntrada(gestorticketCine);
        ca.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTusuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTusuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTusuarioActionPerformed

    private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
       System.exit(0);     }//GEN-LAST:event_cancelarButtonActionPerformed

    private void jBaceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBaceptarKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBaceptarKeyPressed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ControlAcceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ControlAcceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ControlAcceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ControlAcceso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ControlAcceso().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelarButton;
    private javax.swing.JButton jBaceptar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jTcontraseña;
    private javax.swing.JTextField jTusuario;
    private javax.swing.JButton userNoRegistradoButton;
    // End of variables declaration//GEN-END:variables
    private DarAltaPelicula darAltaPelicula;
    public ArrayList<UsuarioGenerico> usuariosGenericos = new ArrayList<>();
    public ArrayList<UsuarioRegistrado> usuariosregistrados = new ArrayList<>();
    public ArrayList<Administrador> administradores = new ArrayList<>();
    private ComprarEntrada comprarEntrada;
    private ConsultarCines consultarCines;
    private static TicketCine tk;
    private GestorTicketCine gestorticketCine;

    public String getContraseña() {
        return jTcontraseña.getText();
    }

    public void setContraseña(JPasswordField contraseña) {
        this.jTcontraseña = contraseña;
    }

    public String getUsuario() {
        return jTusuario.getText();
    }

    public void setUsuario(String usuario) {
        this.jTusuario.setText(usuario);
    }

}
